from document_loader import docs
import sys
from pathlib import Path
sys.path.append(str(Path(__file__).parent.parent))  # 将项目根目录加入Python路径
import json
from llm_func.chat import stream_main
from basic import get_basic_info


# 生成五个维度的分数
def generate_analysis_score():
    # 从get_basic_info获取所有基础数据
    basic_data = get_basic_info()
    
    # 解包需要使用的变量
    enterpriseName = basic_data['enterpriseName']
    industry_name = basic_data['industry_name']
    company_translation = basic_data['company_translation']
    business_products = basic_data['business_products']
    customer = basic_data['customer']
    net_profit = basic_data['net_profit']
    operating_income = basic_data['operating_income']
    operating_costs = basic_data['operating_costs']
    pay_taxes = basic_data['pay_taxes']
    total_industrial = basic_data['total_industrial']
    time_year = basic_data['time_year']
    subsidy_details_str = basic_data['subsidy_details_str']
    staff_total = basic_data['staff_total']
    social_security = basic_data['social_security']
    bachelor_total = basic_data['bachelor_total']
    college_graduates = basic_data['college_graduates']
    completableFuture1 = basic_data['completableFuture1']
    completableFuture2 = basic_data['completableFuture2']
    completableFuture3 = basic_data['completableFuture3']
    completableFuture5 = basic_data['completableFuture5']
    completableFuture6 = basic_data['completableFuture6']
    completableFuture7 = basic_data['completableFuture7']
   
   
    content = f"""
        请根据以下企业数据：

        【企业基本信息】
        - 企业名称：{enterpriseName}
        - 所属行业：{industry_name}
        - 公司简介：{company_translation}
        - 业务和产品：{business_products}
        - 主要客户：{customer}

        【企业财务信息】
        - 净利润：{net_profit}
        - 营业收入（亿元）：{operating_income}
        - 营业成本/研发投入（亿元）：{operating_costs}
        - 纳税（亿元）：{pay_taxes}
        - 工业总产值：{total_industrial}
        - 数据年份：{time_year}

        【企业投资情况】
        {subsidy_details_str}

        【企业就业情况】
        - 职工总数：{staff_total}
        - 社保参保人数：{social_security}
        - 本科学历人数：{bachelor_total}
        - 新增高校毕业人数：{college_graduates}

        【企业风险情况】
        - 荣誉信息：{completableFuture1}
        - 严重失信：{completableFuture2}
        - 经营异常：{completableFuture3}
        - 行政处罚：{completableFuture5}
        - 行政许可：{completableFuture6}
        - 涉诉涉裁：{completableFuture7}

        【企业成长能力】
        专利、著作、软著

        请你根据评分文档内容，对以下五个企业维度进行专业评分，每项满分为100分，评分标准可参考文档中的描述与表现。

        评分维度为：
        - 成长能力
        - 创新能力
        - 经营现状
        - 信用风险
        - 异动风险

        请仅输出如下 JSON 结构：
        {{ 
            "develop": 成长能力得分,
            "innovation": 创新能力得分,
            "status": 经营现状得分,
            "credit": 信用风险得分,
            "abnormal": 异动风险得分
        }}

        字段映射为：
        {{
            "成长能力": "develop",
            "创新能力": "innovation",
            "经营现状": "status",
            "信用风险": "credit",
            "异动风险": "abnormal"
        }}

        请勿添加任何解释或多余文本，只输出 JSON 结果。

        评分参考文档如下：
        {docs}
        """
    return content,enterpriseName


# 解析输出格式——分数
def analysis_score_json():
    try:
        content,enterpriseName = generate_analysis_score()
        messages = [
            {"role": "system", "content": "默认请返回json格式"},
            {"role": "user", "content": content}
            ]
        response = stream_main(messages, stream=False)
        print("*" * 50)
        print("llm返回数据:",response)
        print("*" * 50)

        # ========== 解析与输出 ==========
        radar_score = json.loads(response)
        print("评分 JSON:")
        print(json.dumps(radar_score, indent=2, ensure_ascii=False))


        # 计算分数
        score = 100/5

        if score<59:
            suggestion = "不合格"
        elif score>=60 & score <=74:
            suggestion = "合格"
        elif score>=75 & score <=89:
            suggestion = "良好"
        elif score>=90 & score <=100:
            suggestion = "优秀"

        if score<=33.3:
            recommend = "一般推荐"
        elif score>33.3 & score <= 66.6:
            recommend = "重点推荐"
        elif score>66.6 & score <= 100:
            recommend = "特别推荐"
        
        output_data = {
            "score":score,
            "suggestion":suggestion,
            "enterpriseName":enterpriseName,
            "display": {
                "average": score,
                "recommend": recommend
            },
            "radar_chart": radar_score  # 雷达图数据
        }
        return ""
    except json.JSONDecodeError:
        print("⚠️ 无法解析为 JSON，原始内容如下：")
        print(content)


if __name__ == "__main__":
    out = analysis_score_json()
    print(out)
